package EGS.impl;
import java.util.List;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service10101Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 10101 核对SN的BIN是否一致
	 */
	public EGS_execute_service10101Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();

			if (msgClass.equals("10101")) {// 核对SN的BIN是否一致
//				iSN=Mroot.element("body").element("pcb").attribute("barcode").getValue();
				@SuppressWarnings("unchecked")
				List<Element> pcbs = Mroot.element("body").elements();
				String SNs = "";
				for (int i = 0; i < pcbs.size(); i++) {
					if (i > 0) {
						SNs = SNs + ",";
					}
					SNs = SNs + "'" + pcbs.get(i).attribute("barcode").getValue() + "'";
				}
				SNs = "(" + SNs + ")";

				Mroot.element("body").remove(Mroot.element("body").element("pcb"));
				Element eleR = Mroot.element("body").addElement("result");
				String strsql = "select BSindex from vOrderSerialNumber where serialNumber in " + SNs
						+ " group by BSindex";

				try {
					JTable t = sql.select(strsql);
					if (t == null || t.getRowCount() < 1) {
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", "条码不存在");
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(101010, "" + SNs + "_条码不存在");
					} else if (t.getRowCount() > 1) {
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", "多种BIN");
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(101010, "" + SNs + "_多种BIN");
					} else {
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(101011, "" + SNs);
					}
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(101010, "" + SNs + "_" + str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
